/*pages文件夹内公用*/

//token 失效
function checkToken() {
    layer.msg('token已过期，请重新登录！', {
        icon: 2,
        time: 1500
    });
    setTimeout(function() {
        javascript: parent.signOut2();
    }, 1500)
}

// 查询运营商 
// chooseVal:默认选中id  fun:回调  control:是否查询所有状态运营商 node:写入节点位置，默认id为operator 
// chooseFirst：是否默认选中第一列(优先级低于chooseVal)  
function queryOperator(chooseVal, fun, control, node, chooseFirst) {
    layui.use(['laytpl', 'table', 'element', 'jquery', 'layer'], function() {
        var table = layui.table;
        var element = layui.element;
        var laytpl = layui.laytpl;
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        $('body').off('click').on('click', 'button', function() {
                $(this).blur();
            })
            //查询运营商
        var params1 = $.extend({}, publicData);
        if (!control) {
            params1.compStatus = 1; //查询状态正常运营商
        }
        if (!node) {
            var view = document.getElementById('operator');
        } else {
            var view = document.getElementById(node);
        }
        var allOperator = '';
        $.ajax({
            url: httpUrl + 'companyInfo/queryCompanyInfoByPage',
            type: "POST",
            headers: headData,
            data: JSON.stringify(params1),
            contentType: "application/json",
            dataType: "json",
			async:false,
            success: function(res) {
                // console.log(res);
                if (res.returnCode == 'SUCCESS') {
                    allOperator = res.data.list;
                    if (fun) {
                        fun(allOperator);
                    }
                    var data = { //数据
                        "list": allOperator
                    }
                    if (chooseVal) {
                        data.choose = chooseVal;
                    } else if (chooseFirst) {
                        data.choose = allOperator[0].id;
                    }
                    var getTpl = operatorList.innerHTML;
                    laytpl(getTpl).render(data, function(html) {
                        view.innerHTML = html;
                    });
                    form.render('select'); //select
                } else {
                    if (res.returnCode == 'UNAUTHORIZED') {
                        checkToken();
                    } else {
                        layer.msg(res.returnMsg, {
                            icon: 2
                        });
                    }
                }
            },
            error: function(xhr) {
                layer.msg('网络异常', {
                    icon: 2
                });
                //console.log(xhr); 
            }

        })
    })
}

//查询停车场
// companyId:运营商id  chooseVal:默认选中id  fun:回调 funControl:是否只执行回调 control:是否查询所有状态停车场
// node:写入节点位置，默认id为operator chooseFirst：是否默认选中第一列(优先级低于chooseVal) ; componentChoose:组件id选择
var allParking = ''; //所有停车场
function queryParking(companyId, chooseVal, fun, funControl, control, node, chooseFirst,componentChoose) {
    layui.use(['laytpl', 'table', 'element', 'jquery', 'layer'], function() {
        var table = layui.table;
        var element = layui.element;
        var laytpl = layui.laytpl;
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        //查询停车场
        var params1 = $.extend({}, publicData);
        if (!control) {
            params1.deviceStatus = 1; //查询状态正常停车场
        }
        if (companyId) {
            params1.companyId = companyId;
        }
        if (!node) {
            var view = document.getElementById('parking');
        } else {
            var view = document.getElementById(node);
        }

        $.ajax({
            url: httpUrl + 'userPark/getUserPark',
            type: "POST",
            headers: headData,
            data: JSON.stringify(params1),
            contentType: "application/json",
            dataType: "json",
            success: function(res) {
                //console.log(res);
                allParking = res.data;
                if (res.returnCode == 'SUCCESS') {
                    allParking = res.data;
                    if (fun) {
                        fun(allParking);
                    }
                    if (!funControl) {
                        var data = { //数据
                            "list": allParking
                        }
                        if (chooseVal) {
                            data.choose = chooseVal;
                        } else if (chooseFirst) {
                            data.choose = allParking[0].id;
                        }
                        if(!componentChoose){
                            var getTpl = parkList.innerHTML;
                        }else if(componentChoose==2){
                            var getTpl = parkList2.innerHTML;
                        }
                        laytpl(getTpl).render(data, function(html) {
                            view.innerHTML = html;
                        });
                        form.render('select'); //select
                    }
                } else {
                    if (res.returnCode == 'UNAUTHORIZED') {
                        checkToken();
                    } else {
                        layer.msg(res.returnMsg, {
                            icon: 2
                        });
                    }
                }
            },
            error: function(xhr) {
                layer.msg('网络异常', {
                    icon: 2
                });
                //console.log(xhr); 
            }

        })
    })
}
// 查询车型
function carType(carType,fun) {
    layui.use(['laytpl', 'table', 'element', 'jquery', 'layer'], function() {
        var table = layui.table;
        var element = layui.element;
        var laytpl = layui.laytpl;
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var params7 = $.extend({}, publicData);
        params7.field = 'vehicle_Type';
        params7.Effective = 1;
        $.ajax({
            url: httpUrl + 'base/queryCodeByFieldAndEffective',
            type: "POST",
            headers: headData,
            data: JSON.stringify(params7),
            contentType: "application/json",
            dataType: "json",
            success: function(res) {
                //console.log(res)  
                if (res.returnCode == 'SUCCESS') {
                    var view = document.getElementById('vehicleType');
                    var getTpl = vehicleTypeList.innerHTML;
                    laytpl(getTpl).render(res.data, function(html) {
                        view.innerHTML = html;
                    });
                    form.render('select'); //select
                    if (fun) {
                        fun();
                    }
                } else {
                    //token 失效
                    if (res.returnCode == 'UNAUTHORIZED') {
                        checkToken();
                    } else {
                        layer.msg(res.returnMsg, {
                            icon: 2
                        });
                    }
                }
            },
            error: function(xhr) {
                layer.close(loaderIndex2);
                layer.msg('网络异常', {
                    icon: 2
                });
                //console.log(xhr); 
            }

        })
    })
}
// 查询字典
function getVehicleProp(keyword,idName,fun) {
    layui.use(['laytpl', 'table', 'element', 'jquery', 'layer'], function() {
        var table = layui.table;
        var element = layui.element;
        var laytpl = layui.laytpl;
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var params7 = $.extend({}, publicData);
        params7.field = keyword;
        params7.Effective = 1;
        $.ajax({
            url: httpUrl + 'base/queryCodeByFieldAndEffective',
            type: "POST",
            headers: headData,
            data: JSON.stringify(params7),
            contentType: "application/json",
            dataType: "json",
            success: function(res) {
                //console.log(res)  
                if (res.returnCode == 'SUCCESS') {
                    var view = document.getElementById(idName);
                    var getTpl = vehicleTypeList.innerHTML;
                    laytpl(getTpl).render(res.data, function(html) {
                        view.innerHTML = html;
                    });
                    form.render('select'); //select
                    if (fun) {
                        fun();
                    }
                } else {
                    //token 失效
                    if (res.returnCode == 'UNAUTHORIZED') {
                        checkToken();
                    } else {
                        layer.msg(res.returnMsg, {
                            icon: 2
                        });
                    }
                }
            },
            error: function(xhr) {
                layer.close(loaderIndex2);
                layer.msg('网络异常', {
                    icon: 2
                });
                //console.log(xhr); 
            }

        })
    })
}
//导出
function download(exportparams, URL, name, fileType) {
    exportparams.isOpenPage = false;
    var url = URL;
    var xhr = new XMLHttpRequest();
    xhr.open('POST', url, true); // 也可以使用POST方式，根据接口
    xhr.setRequestHeader("token", headData.token);
    xhr.responseType = "blob"; // 返回类型blob 
    xhr.setRequestHeader('Content-Type', 'application/json');
    // 定义请求完成的处理函数，请求前也可以增加加载框/禁用下载按钮逻辑
    xhr.onload = function() {
        // 请求完成
        if (this.status === 200) {
            // 返回200
            var blob = this.response;
            var a = document.createElement('a');
            if (fileType) {
                a.download = name + fileType;
            } else {
                a.download = name + '.xlsx';
            }
            // console.log(blob)
            a.href = window.URL.createObjectURL(blob)
            var body = document.getElementsByTagName('body')[0];
            body.appendChild(a); // 修复firefox中无法触发click
            a.click();
            body.removeChild(a);
        }
    };
    // 发送ajax请求
    xhr.send(JSON.stringify(exportparams))
}


// 获取aa天之后的日期
function fun_date(aa) {
    var date1 = new Date(),
        time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate(); //time1表示当前时间
    var date2 = new Date(date1);
    date2.setDate(date1.getDate() + aa);
    var time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
    return time2
}

function dateAdd(startDate, day) {
    startDate = new Date(startDate);
    startDate = +startDate + 1000 * 60 * 60 * day;
    startDate = new Date(startDate);
    var nextStartDate = startDate.getFullYear() + "-" + (startDate.getMonth() + 1) + "-" + startDate.getDate();
    return nextStartDate;
}

// 判断time2大于等于time1 range天1s
function judgeTime(time1, time2, range) {
    var range = range;
    if (range) {
        range = range * 1 * 86400000 + 1;
    } else {
        range = 0;
    }
    var date1 = Date.parse(time1);
    var date2 = Date.parse(time2);
    if (date2 - date1 >= range) {
        return true
    } else {
        return false
    }
}
layui.use(['laytpl', 'table', 'element', 'jquery', 'layer'], function() {
    var table = layui.table;
    var element = layui.element;
    var laytpl = layui.laytpl;
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    document.onkeydown = function(e) {
        var theEvent = window.event || e;
        var code = theEvent.keyCode || theEvent.which;
        if (code == 13) {

            $(".query").trigger("click");
        }
    }
})